import { getCategory } from "../../api/api.js"
var app = getApp();
Page({
  data: {
    'curr': 0
  },
  onChangeTab: function (event) {
    this.setData({
      'curr': event.currentTarget.dataset.index
    });
  },
  onShowBook: function (event) {
    var id = event.currentTarget.dataset.id;
    var name = event.currentTarget.dataset.name;
    my.navigateTo({
      url: '/pages/list/list?id=' + id + '&name=' + name
    });
  },
  onGoSearch: function () {
    my.navigateTo({
      url: '/pages/search/search'
    });
  },
  onLoad: function () {
    var category = my.getStorageSync({key: 'category'});
    if (category['data']) {
      this.setData({
        'category': category['data']
      });
    } else {
      my.showLoading({
        title: '加载中...'
      });
      var that = this;
      var data = new Array();

      getCategory().then(res => {
        that.callBack(res)
      })
    
    }
  },

  /**
   * 回调
   */
  callBack: function (ret) {
    this.setData({
      'category': ret.result
    });
    let res = my.setStorageSync({key: 'category', data: ret.result});
    my.hideLoading();
  },

  enterNcov: function(){
    my.navigateToMiniProgram({
      appId: '2021001132600368',
    });
  },
  enterNcovCookbook: function(){
    my.navigateTo({
      url: "/pages/list/list?id=165&name=肺炎"
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '神奇菜谱',
      path: '/pages/index/index',
      success: function (res) {
        my.showToast({
          content: '分享成功'
        });
      },
      fail: function (res) {// 转发失败
      }
    };
  }
});